import { throwErrorMessage } from '@/store/utils/api'
import ConversationApi from '../../../../api/inbox/conversation'
import mutationTypes from '../../../mutation-types'

export default {
  markMessagesRead: async ({ commit }, data) => {
    try {
      const {
        data: { id, agentLastSeenAt: lastSeen },
      } = await ConversationApi.markMessageRead(data)
      setTimeout(
        () =>
          commit(mutationTypes.UPDATE_MESSAGE_UNREAD_COUNT, { id, lastSeen }),
        4000
      )
    } catch (error) {
      // Handle error
    }
  },

  markMessagesUnread: async ({ commit }, { id }) => {
    try {
      const {
        data: { agentLastSeenAt: lastSeen, unreadCount: unreadCount },
      } = await ConversationApi.markMessagesUnread({ id })
      commit(mutationTypes.UPDATE_MESSAGE_UNREAD_COUNT, {
        id,
        lastSeen,
        unreadCount,
      })
    } catch (error) {
      throwErrorMessage(error)
    }
  },
}
